import Vue from "vue";
import Vuex from "vuex";
import { bonusOdd, getUserInfo, winNumber } from "./service/api";

Vue.use(Vuex);
const amount_model_data = [
  { text: "元", value: 1, price: 2 },
  { text: "角", value: 2, price: 0.2 },
  { text: "分", value: 3, price: 0.02 },
  { text: "厘", value: 4, price: 0.002 }
];

export default new Vuex.Store({
  state: {
    token: localStorage.getItem("token") || null,
    uid: 0,
    name: "",
    avatar: "",
    titlename: "五星复式", //headername   titlename
    offon:false , //点击底部的遮挡板收起

    cart_list: [], //号码篮子
    bonus_odd: {}, //奖金赔率
    user_info: {}, //会员信息
    win_number: {}, //开奖号码
    current_play_type: "五星直选复式",
    amount_model: 1, //玩法模式
    multiple: 1, //倍率
    credit_cart: [], //信用篮子

    is_bet: true, // 是否可以下注
  },
  getters: {
    amount_model_price(state) {
      let obj = amount_model_data.filter(
        item => item.value === state.amount_model
      );
      return obj[0].price;
    },
    amount_model_text(state) {
      let obj = amount_model_data.filter(
        item => item.value === state.amount_model
      );
      return obj[0].text;
    }
  },
  mutations: {
     // 改变header标题
     changeTitle(state, titlename) {
      state.titlename = titlename
    },
    // 遮挡板开关
    changeOffon(state, offon) {
      state.offon = offon
    },
    setUser(state, payload) {
      let { uid, username, avatar } = payload;
      state.uid = uid;
      state.username = username;
      state.avatar = avatar;

      localStorage.setItem("uid", uid + "");
      localStorage.setItem("username", username);
      localStorage.setItem("avatar", avatar);
    },
    setToken(state, token) {
      state.token = token;
    },
    setAvatar(state, avatar) {
      state.avatar = avatar;
      localStorage.setItem("avatar", avatar);
    },
    set_cart_list(state, list_item) {
      state.cart_list = [...state.cart_list, list_item];
    },
    replace_cart_list(state, list) {
      state.cart_list = list;
    },
    set_bonus_odd(state, bonus_odd) {
      state.bonus_odd = bonus_odd;
    },
    set_user_info(state, user_info) {
      state.user_info = user_info;
    },
    set_win_number(state, win_number) {
      state.win_number = win_number;
    },
    set_amount_model(state, amount_model) {
      state.amount_model = amount_model;
    },

    set_multiple(state, multiple) {
      state.multiple = multiple;
    },
    set_current_play_type(state, current_play_type) {
      state.current_play_type = current_play_type;
    },
    set_credit_cart(state, list){
      state.credit_cart = [...state.credit_cart, ...list];
    },
    replace_credit_cart(state, list) {
      state.credit_cart = list;
    },
    set_is_bet(state, flag){
      state.is_bet = flag;
    }
  },
  actions: {
    login({ commit }, payload) {
      let { token, id, username, avatar } = payload;

      commit("setToken", token);
      commit("setUser", { uid: id, username, avatar });
    },

    async asyncBonusOdd({ commit }) {
      let res = await bonusOdd();
      if (res.data.status_code === 200) {
        commit("set_bonus_odd", res.data.data);
      }
    },

    async asyncGetUserInfo({ commit }) {
      let res = await getUserInfo();
      if (res.data.status_code === 200) {
        commit("set_user_info", res.data.data);
      }
    },

    async asyncWinNumber({ commit }) {
      let res = await winNumber();
      if (res.data.status_code === 200) {
        commit("set_win_number", res.data.data.data[0]);
      }
    }
  }
});
